<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */
 
class BlogController extends Zenddn_Controller_Action{
	
    public function init(){
		parent::init();
        parent::loadTemplate('magashop');
        $this->view->page = "blog";
	}
	
	public function indexAction(){
	    $request = $this->_request;
        
	    $setting_model = new Default_Model_Setting();
        $setting = $setting_model->getSetting('general');
        
        $posts_per_page = 5;
        
        if($setting){
            $setting = unserialize($setting);
            $posts_per_page = $setting['posts_per_page'];
        }
        
        $setting = $setting_model->getSetting('page');
        
        $model = new Default_Model_Post();
        $select = $model->getQuery();
        
        if($setting){
            $setting = unserialize($setting);
            $category = $setting['blog'];
            
            $category_id = implode(",", $category);
            $select = $model->getQuery(array('where' => "category IN ($category_id)"));
        }
        
	    $currentPage = $request->getParam('page', 1);
        $pageRange = 3;
        
        $adapter = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($adapter);
        
        $paginator->setItemCountPerPage($posts_per_page);
        $paginator->setPageRange($pageRange);
        $paginator->setCurrentPageNumber($currentPage);
        
        $this->view->data = $paginator;
	}
    
    public function detailAction(){
        $request = $this->_request;
        $id = $request->getParam('id', false);
        
        if($id){
            $model = new Default_Model_Post();
            $post = $model->getData($id);
            
            if($post){
                $this->view->data = $post;
            }else{
                $this->_helper->_redirector->gotoRoute(array(), 'error', true);
            }
        }else{
            $this->_helper->_redirector->gotoRoute(array(), 'error', true);
        }
    }
}